$(document).ready(function(e) {
    if(sessionStorage.getItem('mobile')!= '' && sessionStorage.getItem('mobile')){
        $('#user').show();
        $('#user span').text(sessionStorage.getItem('mobile'));
        $('.login_module').hide();
    }
    $("#getCode").click(MathRand);
    $("#loginR").click(submitMobile);
});
var timeNumber = 60;
var set_timer;
function setTime() {
    if(timeNumber === 0){
        $('#getCode').text('获取验证码');
        timeNumber = 60;
        clearInterval(set_timer);
        $('#getCode').bind('click',MathRand);
    }else {
        timeNumber --;
        $('#getCode').text('重新发送('+timeNumber+')');
        $('#getCode').unbind('click',MathRand);
    }
}

function MathRand() {
    var sMobile = $("#mobile").val();
    if (sMobile === "" || sMobile === null) {
        alert('手机号不能为空');
        $('#mobile').attr('placeHolder','');
    } else {
        if (!(/^1\d{10}$/.test(sMobile))) {
            alert('手机号格式不正确');
            $('#mobile').attr('placeHolder','');
        } else {
            var mobile = $("#mobile").val();
            $.ajax({
                url: "http://www.rainbow-bus.cn/rainbow/app/ph/setrandomNumber",
                type: 'post',
                dataType: 'json',
                data: {
                    mobile: mobile
                },
                success: function(result) {
                   // alert('sss');
                    setTime();
                    set_timer =  setInterval(setTime,1000);
                }
            });
        }
    }
}

function submitMobile() {
    var mobile = $("#mobile").val();
    var randomNumber = $("#identifyCode").val();
    if (mobile === null || mobile === "") {
        alert('手机号不能为空');
        $('#mobile').attr('placeHolder','');
    }else {
        $.ajax({
            url: "http://www.rainbow-bus.cn/rainbow/app/ph/loginOrRegister",
            type: 'post',
            dataType: 'json',
            data: {
                "mobile": mobile,
                "randomNumber": randomNumber
            },
            success: function(result) {
                if (result.success) {
                    sessionStorage.setItem("mobile",mobile);
                    $('#user').show();
                    $('#user span').text(mobile);
                    $('.login_module').hide();
                } else {
                    /* dialog("提示", result.msg);*/
                    if(result.msg === '请输入正确的验证码'){
                      alert('验证码错误');
                    }else {
                        alert('请输入手机号');
                    }
                }
            }
        });
    }
}
